package org.jeecg.common.util.sm.bean;

import lombok.Getter;
import lombok.Setter;

import java.security.SecureRandom;

@Setter
@Getter
public class KeyAndIV {
    private byte[] key;
    private byte[] iv;

    public KeyAndIV(byte[] key, byte[] iv) {
        this.key = key;
        this.iv = iv;
    }


    //生成sm4加密key和初始向量
    public static KeyAndIV generateKeyAndIV(int ivLength,int keyLength) {
        byte[] buffer = new byte[ivLength + keyLength];
        SecureRandom random = new SecureRandom();
        random.nextBytes(buffer);
        byte[] key = new byte[keyLength];
        System.arraycopy(buffer, 0, key, 0, key.length);
        byte[] iv;
        iv = new byte[ivLength];
        System.arraycopy(buffer, key.length, iv, 0, iv.length);
        return new KeyAndIV(key, iv);
    }
}
